package com.tianzeculture.stagebao.activity.personal.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tianzeculture.stagebao.R;
import com.tianzeculture.stagebao.bean.FansBean;
import com.tianzeculture.stagebao.util.Util;

import java.util.ArrayList;
import java.util.List;

import de.hdodenhof.circleimageview.CircleImageView;

import static com.tianzeculture.stagebao.net.base.AppBaseRestUsageV2.PICTURE_URL;

/**
 * Created by Administrator on 2018/6/19 0019.
 */

public class FocusAdapter extends BaseAdapter {
    Context context;
    List<FansBean> list = new ArrayList<>();

    boolean isfans;

    public FocusAdapter(Context context, boolean isfans) {
        this.context = context;
        this.isfans = isfans;
    }

    public void setList(List<FansBean> fansList) {
        list.clear();
        list.addAll(fansList);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder holder = null;
        if (view == null) {
            holder = new ViewHolder();
            view = LayoutInflater.from(context).inflate(R.layout.item_focus, null);
            holder.ivHead = view.findViewById(R.id.ivHead);
            holder.tvName = view.findViewById(R.id.tv_name);
            holder.tvContent = view.findViewById(R.id.tv_content);
            holder.tvFocus = view.findViewById(R.id.button_focus);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        setData(holder, i);
        return view;
    }

    private void setData(final ViewHolder holder, final int i) {
        if (list.get(i).getUser() == null)
            return;
        Glide.with(context).load(PICTURE_URL + list.get(i).getUser().getImgurl()).placeholder(R.mipmap.icon_head).error(R.mipmap.icon_head).into(holder.ivHead);
        holder.tvName.setText(list.get(i).getUser().getNickname());
        holder.tvContent.setText(("1".equals(list.get(i).getUser().getUserType()) ? "商家:" : "艺人") + list.get(i).getUser().getAddress());
        if (isfans) {
            if ("1".equals(list.get(i).getIsAttention())) {
                holder.tvFocus.setText("已关注");
                holder.tvFocus.setTextColor(context.getResources().getColor(R.color.colorPink));
                holder.tvFocus.setBackgroundResource(R.drawable.bg_red_transparent_round);
            } else {
                holder.tvFocus.setText(" + 关注");
                holder.tvFocus.setTextColor(context.getResources().getColor(R.color.colorGray_777));
                holder.tvFocus.setBackgroundResource(R.drawable.bg_gray_white_round);
            }
        } else {
            holder.tvFocus.setText("已关注");
            holder.tvFocus.setTextColor(context.getResources().getColor(R.color.colorPink));
            holder.tvFocus.setBackgroundResource(R.drawable.bg_red_transparent_round);
        }
        holder.tvFocus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if ("已关注".equals(holder.tvFocus.getText())) {
                    if (!isfans) {


                        holder.tvFocus.setText(" + 关注");
                        holder.tvFocus.setTextColor(context.getResources().getColor(R.color.colorGray_777));
                        holder.tvFocus.setBackgroundResource(R.drawable.bg_gray_white_round);
                        listener.doAttention(list.get(i), 1);
                    }
                } else {
                    if (isfans) {
                        holder.tvFocus.setText("已关注");
                        holder.tvFocus.setTextColor(context.getResources().getColor(R.color.colorPink));
                        holder.tvFocus.setBackgroundResource(R.drawable.bg_red_transparent_round);
                        listener.doAttention(list.get(i), 0);
                    }
                }
            }
        });
    }

    public void setListener(AttentionListener listener) {
        this.listener = listener;
    }

    AttentionListener listener;

    public interface AttentionListener {
        void doAttention(FansBean fansBean, int requestType);
    }

    private class ViewHolder {
        public CircleImageView ivHead;
        public TextView tvName;
        public TextView tvContent;
        public TextView tvFocus;
    }
}
